package edu.uoc.tds.pac4.client.ss1;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;

import edu.uoc.tds.pac4.beans.Usuari;
import edu.uoc.tds.pac4.client.Client;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;

public class PantallaModUsuaris extends PantallaUsuaris implements PanelClientInterface{

	private static final long serialVersionUID = 1L;
	private JPanel jPanelContainer = null;
	private JPanelLlistatUsuaris jPanelLlistat = null;
	private JButton jButtonCargar = null;
	private JButton jButtonModificar = null;
	private JButton jButtonEliminar = null;
	Vector<Usuari> currentUsuari = null;
	private Client cliente = null;
	
	/**
	 * Constructor
	 */
	public PantallaModUsuaris() {
		super();
		initialize();
	}
	
	/**
	 * Constructor que rep una referencia al servidor remot
	 * 
	 * @param remot
	 */
	public PantallaModUsuaris(Client cliente,  RemoteInterface remot) {
		this.cliente = cliente;
		this.remot = remot;
		initialize();
	}

	/**
	 * Inicialitza la pantalla
	 */
	protected void initialize() {
		// this.setTitle("Pantalla.title");
	}

	@Override
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
			jPanelContainer = new JPanel();
			jPanelContainer.setSize(dimension);
			jPanelContainer.setLayout(new GridBagLayout());
			
			jPanelContainer.add(getjButtonCargar(), null);
			jPanelContainer.add(getjButtonModificar(), null);
			jPanelContainer.add(getjButtonEliminar(), null);
			
			GridBagConstraints gbcPanelUsuari = new GridBagConstraints();
			gbcPanelUsuari.gridx = 1;
			gbcPanelUsuari.gridy = 1;
			gbcPanelUsuari.weightx = 1;
			gbcPanelUsuari.weighty = 1;
			gbcPanelUsuari.insets = new Insets(0, 10, 10, 10);
			gbcPanelUsuari.fill = GridBagConstraints.BOTH;

			jPanelContainer.add(getjPanelLlistat(), gbcPanelUsuari);	
		}
		return jPanelContainer;
	}
	
	/**
	 * This method initializes jPanelUsuari
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getjPanelLlistat() {
		if (jPanelLlistat == null) {
			jPanelLlistat = new JPanelLlistatUsuaris();
		}
		return jPanelLlistat;
	}
	
	/**
	 * Actualitza el contingut del llistat i la taula
	 */
	public void updateTaula() {
		
		
		// crida al servidor per rebre els events
		try {
			currentUsuari = remot.getUsuaris(true);
		} catch (RemoteException e) {
			new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
		} catch (GestioEventsException e) {
			new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
		}
		jPanelLlistat.drawUsuaris(currentUsuari);
	}
	
	/**
	 * This method initializes jButtonCargar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getjButtonCargar() {

		if (jButtonCargar == null) {
			jButtonCargar = new JButton("Cargar");
			jButtonCargar.setBounds(new Rectangle(85, 195, 100, 25));
			jButtonCargar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		

					try {
						updateTaula();

					} catch (Exception e){
						// Pending verificar
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButtonCargar;
	}
	
	/**
	 * This method initializes jButtonModificar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getjButtonModificar() {

		if (jButtonModificar == null) {
			jButtonModificar = new JButton("Modificar");
			jButtonModificar.setBounds(new Rectangle(85, 195, 100, 25));
			jButtonModificar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		

					try {
						new PantallaModificacioUsuaris(cliente, remot,currentUsuari.get(jPanelLlistat.getSelectedRow()));

					} catch (Exception e){
						// Pending verificar
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButtonModificar;
	}
	
	/**
	 * This method initializes jButtonEliminar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getjButtonEliminar() {

		if (jButtonEliminar == null) {
			jButtonEliminar = new JButton("Eliminar");
			jButtonEliminar.setBounds(new Rectangle(85, 195, 100, 25));
			jButtonEliminar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		

					try {
						Usuari usuari = currentUsuari.get(jPanelLlistat.getSelectedRow());
						usuari.baixa(new Date(), "Eliminat desde pantalla");
						remot.insertUpdateUsuari(usuari, false);
						new PantallaError("Info_baixa", PantallaError.Tipus.INFO);  

					} catch (Exception e){
						// Pending verificar
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButtonEliminar;
	}
}
